<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" :model="queryForm" class="demo-form-inline">
        <el-form-item label="项目">
          <el-select
            filterable
            clearable
            v-model="queryForm.appUuid"
            placeholder="请选择项目">
            <el-option
              v-for="item in appOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目配置Key名称">
          <el-input v-model="queryForm.appConfigName" placeholder="模糊查询"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="addNew">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="pageResult.data"
        border
        :default-sort="{prop: 'updateTime', order: 'descending'}"
        style="width: 100%; margin-top: 20px;font-size: 10px">
        <el-table-column
          align="center"
          label="项目">
          <template slot-scope="scope">
            {{ getAppName(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="appConfigName"
          label="配置Key">
        </el-table-column>
        <el-table-column
          align="center"
          prop="appConfigValue"
          label="配置值">
        </el-table-column>
        <el-table-column
          align="center"
          prop="desc"
          label="描述">
        </el-table-column>
        <el-table-column
          align="center"
          label="是否有效">
          <template slot-scope="scope">
            <span v-if="scope.row.isDelete === 1">无效</span>
            <span v-if="scope.row.isDelete === 0">有效</span>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          align="center"
          width="300"
          label="创建/修改时间">
          <template slot-scope="scope">
            {{ ddkDateFormat(scope.row.createTime) }} / {{ ddkDateFormat(scope.row.updateTime) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="100"
          label="状态">
          <template slot-scope="scope">
            <span style="color: red" v-if="scope.row.isDelete === 1"><strong>离线</strong></span>
            <span style="color: #189c1a" v-if="scope.row.isDelete === 0"><strong>在线</strong></span>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          width="200"
          label="操作">
          <template slot-scope="scope">
            <el-button style="margin-left: 0px" type="text" size="mini" @click="toCopy(scope.row)">复制</el-button>
            <el-button style="margin-left: 0px;color: blueviolet" type="text" size="mini" @click="toPublish(scope.row)">发布</el-button>
            <el-button style="margin-left: 0px" type="text" size="mini" @click="toEdit(scope.row)">编辑</el-button>
            <el-button style="margin-left: 0px;color: #189c1a" type="text" size="mini" @click="toDisableOrEnable(scope.row)">
              <span v-if="scope.row.isDelete === 1">启用</span>
              <span style="color: red" v-if="scope.row.isDelete === 0">禁用</span>
            </el-button>
            <!-- TODO 暂时不支持删除 -->
            <!--            <el-button style="margin-left: 0px;color: red" type="text" size="mini" @click="deleteOne(scope.row)">删除</el-button>-->
            <vue-log module="DDK_APP_CONFIG" :target-id="scope.row.uuid"></vue-log>
          </template>
        </el-table-column>
      </el-table>

      <el-footer>
        <vue-page :paginator="pageResult.paginator" :query="query"></vue-page>
      </el-footer>
    </el-card>
    <app-config-form ref="appConfigForm"></app-config-form>
  </div>
</template>

<script>

import appConfigForm from "./dialog/appConfigForm";
import vuePage from "../common/vue-page.vue"
import vueLog from "../common/vue-log.vue"
import {loadAppSelector} from "../../api/loadSelectorApi";
import {appConfigQuery} from "../../api/appConfigApi";
import {publishDict} from "../../api/registerApi";

export default {
  name: "appConfig",
  components: {
    appConfigForm,
    vuePage,
    vueLog
  },
  data() {
    return {
      queryForm: {
        appId: '',
        appName: '',
        teamUuid: '',
      },
      appOptions: [],
      // 查询返回结果
      pageResult: {
        data: [],
        paginator: {
          currentPage: 0,
          totalCount: 0,
          pageSize: 10,
          totalPage: 0
        }
      },
      logTargetId: undefined,
    }
  },
  created() {
    this.query()
  },
  methods: {
    async query() {
      let vm = this
      vm.appOptions = await loadAppSelector()
      let ret = await appConfigQuery(vm.queryForm, vm.pageResult.paginator)
      if (ret.ok) {
        vm.pageResult = ret.data.queryPage
      } else {
        vm.$message({type: 'error', message: '查询失败：' + ret.message});
      }
    },
    addNew() {
      this.$refs.appConfigForm.load();
    },
    toCopy(row) {
      let clone = this.$cloneObj(row);
      clone.id = undefined
      this.$refs.appConfigForm.load(clone);
    },
    async toPublish(row) {
      let ret = await publishDict(row.uuid)
      if (ret.ok) {
        this.$message({type: 'success', message: '发布成功!'});
      } else {
        this.$message({type: 'error', message: '发布失败：' + ret.message});
      }
    },
    toEdit(row) {
      this.$refs.appConfigForm.load(row);
    },
    getAppName(row) {
      let vm = this
      for (let i = 0; i < vm.appOptions.length; i++) {
        let appOption = vm.appOptions[i];
        if (appOption.value === row.appUuid) {
          return appOption.label
        }
      }
      return row.appUuid;
    },
    /**
     * TODO 下线需要进行验证，暂时没做
     *
     * @param row
     */
    toDisableOrEnable(row) {
      let vm = this
      let clone = vm.$cloneObj(row);
      clone.isDelete = clone.isDelete ^ 1
      vm.$confirm('是否确认修改为【' + (clone.isDelete === 1 ? '无效' : '有效') + '】', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs.appConfigForm.doSave(clone);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    /**
     * TODO 删除的逻辑比较复杂，暂不处理
     *
     * @param row row
     */
    deleteOne(row) {

    }
  }
}
</script>

<style scoped>

</style>
